package com.kgcx.coupon.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.utils.DateTimeUtils;
import com.kgcx.coupon.domain.Punish;
import com.kgcx.coupon.mapper.PunishMapper;
import com.kgcx.coupon.rabbitmq.producer.OpcProducer;
import com.kgcx.dbutils.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/12/7 15:08
 * @since 0.1
 */
@Service
public class PunishService {

    public static Logger logger = LoggerFactory.getLogger(PunishService.class);

    @Autowired
    private PunishMapper punishMapper;
    @Autowired
    private OpcProducer opcProducer;
    /**
     * 分页查询处罚信息
     * @param rd
     * @return
     */
    public List<Punish> queryPunishWithPage(RequestData rd){
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<Punish> list = punishMapper.selectPunishList(rd);
        for (Punish pun :list){
            opcProducer.sendPunish(pun);
        }
        return list;

    }

    /**
     * 新加处罚信息
     * @param licenseId
     * @param punishReason
     * @param punishResult
     * @return
     */
    public Integer  addPunish(String licenseId,String punishReason, String punishResult){
        Punish punish = new Punish();
        punish.setLicenseId(licenseId);
        punish.setPunishReason(punishReason);
        punish.setPunishResult(punishResult);
        long dateTime =  DateTimeUtils.convertNumberSec(LocalDateTime.now());
        punish.setPunishTime(dateTime);
        Integer one = punishMapper.insert(punish);
        try {
            opcProducer.sendPunish(punish);
        }catch (Exception e){
            logger.error("licenseId:"+licenseId+"punish send punish error",e);
        }
        return one;
    }
}
